package com.ybg.user.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.ybg.common.entity.Message;
import com.ybg.user.business.entity.UserRole;
import com.ybg.user.business.service.FrontUserService;
import com.ybg.user.business.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "/admin/user",tags = {"用户管理"})
@RestController
@RequestMapping("/admin/user")
@SaCheckRole
public class UserAdminController {
    private UserService userService;
    private FrontUserService frontUserService;

    @Autowired
    public UserAdminController(UserService userService,
                               FrontUserService frontUserService) {
        this.userService = userService;
        this.frontUserService = frontUserService;
    }

    @ApiOperation(value = "/page",tags = {"管理员分页查看用户信息"})
    @GetMapping("/page/{pn}/{ps}")
    public @ApiResponse(description = "一页用户信息") Message adminUserPage(
            @ApiParam("pageNo") @PathVariable("pn") int pageNo,
            @ApiParam("pageSize") @PathVariable("ps") int pageSize){
        return frontUserService.adminUserPage(pageNo, pageSize);
    }

    @ApiOperation(value = "/add",tags = {"人事分配账号"})
    @PutMapping("/add")
    public @ApiResponse(description = "分配账号结果") Message userAdd(
            @ApiParam("userName") @RequestParam("userName") String userName){
        return userService.adminUserAdd(userName);
    }

    @ApiOperation(value = "/reset",tags = {"管理员重置用户密码"})
    @PostMapping("/reset")
    public @ApiResponse(description = "重置结果") Message adminUserResetPass(
            @ApiParam("userId") @RequestParam("userId") int userId){
        return userService.adminUserResetPass(userId);
    }

    @ApiOperation(value = "/disable",tags = {"人事禁用账号"})
    @PostMapping("/disable")
    public @ApiResponse(description = "禁用账号结果") Message userDisable(
            @ApiParam("userId") @RequestParam("userId") int userId){
        return userService.adminUserDisable(userId);
    }

    @ApiOperation(value = "/enable",tags = {"人事启用账号"})
    @PostMapping("/enable")
    public @ApiResponse(description = "解禁账号结果") Message userEnable(
            @ApiParam("userId") @RequestParam("userId") int userId){
        return userService.adminUserEnable(userId);
    }

    @ApiOperation(value = "/emp",tags = {"根据员工编号提取员工信息"})
    @GetMapping("/emp")
    public @ApiResponse(description = "员工编号关联的员工信息") Message getEmployeeById(
            @ApiParam("empId") @RequestParam("empId") int employeeId){
        return userService.getEmployeeById(employeeId);
    }

    @ApiOperation(value = "/bound/emp",tags = {"用户绑定员工"})
    @PutMapping("/bound/emp")
    public @ApiResponse(description = "用户绑定员工操作结果") Message userBindEmployee(
            @ApiParam("userId") @RequestParam("userId") int userId,
            @ApiParam("empId") @RequestParam("empId") int employeeId){
        return userService.adminBindUserEmployee(userId,employeeId);
    }

    @ApiOperation(value = "/roles",tags = {"用户查看绑定角色关系"})
    @GetMapping("/roles")
    public @ApiResponse(description = "用户与关系结果") Message roleUserCurr(
            @ApiParam("userId") @RequestParam("userId") int userId){
        return userService.roleUserCurr(userId);
    }

    @ApiOperation(value = "/bound/roles",tags = {"用户绑定角色"})
    @PutMapping("/bound/roles")
    public @ApiResponse(description = "用户绑定角色绑定结果") Message userBindRole(
            @ApiParam("UserRole") @ModelAttribute("UserRole") UserRole userRole){
        return userService.adminBindUserRoles(userRole);
    }

}
